package com.cy.service.impl;

import java.util.UUID;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cy.common.exception.ServiceException;
import com.cy.dao.SysUserDao;
import com.cy.entity.SysUser;
import com.cy.service.SysUserService;

/**  
 * @Title:  SysUserServiceImpl.java   
 * @Package com.cy.service.impl   
 * @Description:    描述   
 * @author: ZHANGSIMIN     
 * @date:   2019年10月19日 下午12:59:46   
 * @version V1.0 
 */
@Service
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserDao sysUserDao;

	@Override
	public Integer saveObject(SysUser entity) {
		if(entity==null)
			throw new ServiceException("服务器炸了");
		if(StringUtils.isEmpty(entity.getUsername()) || 
				StringUtils.isEmpty(entity.getPassword()))
			throw new ServiceException("用户名或密码不能为空");
		//2.保存用户自身信息
		//2.1对密码进行加密
		String source=entity.getPassword();
		String salt=UUID.randomUUID().toString();
		SimpleHash sh=new SimpleHash(//Shiro框架
				"MD5",//algorithmName 算法
				source,//原密码
				salt, //盐值
				1);//hashIterations表示加密次数
		entity.setSalt(salt);
		entity.setPassword(sh.toHex());
		int rows=sysUserDao.insertObject(entity);
		//4.返回结果
		return rows;
	}

	@Override
	public SysUser findUser(String username) {
		// TODO Auto-generated method stub
		return sysUserDao.findUserByUserName(username);
	}

}
